#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>


int main(int argc,char **argv){

   char buf[BUFSIZ];
   char clientIP[BUFSIZ];
   struct sockaddr_in serv_addr,client_addr;
   socklen_t client_sock_len;

    //将ip地址转换成struct sockaddr的标准
   char serverIp[] = "127.0.0.1";
   in_addr_t n_addr = inet_addr(serverIp);

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_port = htons(9999);
   serv_addr.sin_addr.s_addr = n_addr;
    // serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);


   int sfd = socket(AF_INET,SOCK_STREAM,0);
   bind(sfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
   //设置可同时链接的数目
   listen(sfd ,10);

   client_sock_len = sizeof(client_addr);
   char  start[] = "server is start...";
   printf("server is start...\n");
   write(STDOUT_FILENO,start,sizeof(start));
   int cfd = accept(sfd,(struct sockaddr *)&client_addr,&client_sock_len);
//    fprintf(STDIN_FILENO,"%s/n","client is connect");
    printf("client ip %s\n",inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,clientIP,sizeof(clientIP)));

   if(cfd == -1){
    perror("accept error");
    exit(1);
   };
    
    int ret;
    char ee[] = "exit\n";
    while(1){
       ret = read(cfd, buf,sizeof(buf));
       printf("strcmp %d\n",strcmp(buf,ee));
       if(strcmp(buf,ee) == 0) exit(1);
        write(STDOUT_FILENO,buf,ret);
       write(cfd,buf,ret);

    }

    

   return 0;
}